package com.yiyi.credit.manage.sso.service.inner;

import com.yiyi.credit.manage.sso.cache.RedisManager;
import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.common.page.PageUtils;
import com.yiyi.credit.manage.sso.common.page.Pagination;
import com.yiyi.credit.manage.sso.mapper.SysUserRoleMapper;
import com.yiyi.credit.manage.sso.model.entity.SysUserRole;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;


@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    RedisManager redisManager;


    @Override
    public Pagination getSysUserRolePageBySysId(Long sysInfoId, String userName, Pagination pagination) throws ServiceException {

        Long count = sysUserRoleMapper.getCount(sysInfoId, userName);
        pagination.setRecordsTotal(count);
        pagination.setPage(PageUtils.getPage(count, pagination.getLength()));
        if (count > Constant.LONG_ZERO) {
            pagination.setData(sysUserRoleMapper.getPageData(sysInfoId, userName, pagination));
        }
        return pagination;
    }


    @Override
    public int addSysUserRole(SysUserRole sysUserRole) throws ServiceException {
        //参数校验
        if (!validParams(sysUserRole)) {
            throw new ServiceException(ResponseCode.PARAM_NOT_PASS);
        }
        sysUserRole.setCreateTime(new Date().getTime());
        sysUserRole.setUpdateTime(new Date().getTime());
        return sysUserRoleMapper.insertSelective(sysUserRole);
    }


    @Override
    public int updateSysUserRole(SysUserRole sysUserRole) throws ServiceException {
        //参数校验
        if (!validParams(sysUserRole)) {
            throw new ServiceException(ResponseCode.PARAM_NOT_PASS);
        }
        sysUserRole.setUpdateTime(new Date().getTime());
        return sysUserRoleMapper.updateByPrimaryKeySelective(sysUserRole);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchAssignSysUserRole(String userIds, Long sysInfoId, Long roleId) throws ServiceException {
        //判断用户userIds
        if (StringUtils.isBlank(userIds)) {
            throw new ServiceException(ResponseCode.PARAM_NOT_PASS);
        }
        String[] userIdList = userIds.split(Constant.SPLIT_ATTR);
        if (userIdList.length == Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.PARAM_NOT_PASS);
        }

        //循环用户userIdList，更新或者新增用户角色
        for (String userId : userIdList) {
            //根据用户userId,sysInfoId查询用户角色表sys_user_role
            List<SysUserRole> sysUserRoleList = getSysUserRole(Long.parseLong(userId), sysInfoId);
            if (null != sysUserRoleList && sysUserRoleList.size() > Constant.INT_ZERO) {
                //存在角色信息 更新
                SysUserRole sysUserRole = sysUserRoleList.get(Constant.INT_ZERO);
                sysUserRole.setRoleId(roleId);
                updateSysUserRole(sysUserRole);
            } else {
                //不存在 新增
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(Long.parseLong(userId));
                sysUserRole.setSysInfoId(sysInfoId);
                sysUserRole.setRoleId(roleId);
                addSysUserRole(sysUserRole);
            }
        }
    }


    @Override
    public List<SysUserRole> getSysUserRole(Long userId, Long sysInfoId) throws ServiceException {
        Example example = new Example(SysUserRole.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("sysInfoId", sysInfoId);
        return sysUserRoleMapper.selectByExample(example);
    }


    @Override
    public List<SysUserRole> getSysUserRoleByRoleId(Long sysRoleId) throws ServiceException {
        Example example = new Example(SysUserRole.class);
        example.createCriteria().andEqualTo("roleId", sysRoleId);
        return sysUserRoleMapper.selectByExample(example);
    }


    @Override
    public int deleteSysUserRoleById(Long id) throws ServiceException {
        return sysUserRoleMapper.deleteByPrimaryKey(id);
    }


    @Override
    public SysUserRole getSysUserRoleById(Long id) throws ServiceException {
        return sysUserRoleMapper.selectByPrimaryKey(id);
    }


    private boolean validParams(SysUserRole sysUserRole) {

        return !(null == sysUserRole.getSysInfoId() || null == sysUserRole.getRoleId() || null == sysUserRole.getUserId());
    }
}
